﻿#include "GameControl.h"


// ids for our GUI controls.
#define STATIC_ID_1  1
#define STATIC_ID_2  2
#define STATIC_ID_3  3
#define STATIC_ID_4  4
#define STATIC_ID_5  5
#define STATIC_ID_6  6


#define BUTTON_ID_1  10
#define BUTTON_ID_2  11
#define BUTTON_ID_3  12
#define BUTTON_ID_4  13


INT GameControl::GameState = 0;

GameControl::GameControl() {
	//ULONG_PTR gdiplusToken;
	Gdiplus::GdiplusStartupInput gdiplusStartupInput;
	Gdiplus::GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);

}

GameControl::~GameControl() {

	Gdiplus::GdiplusShutdown(gdiplusToken);

	SoundWave::Close();
};

BOOL GameControl::InitObject() {

	if (FALSE == _Input.Init(_hWnd, _hInstance)) {
		return FALSE;
	}

	InitVertexBuff();

	_Place.LoadMap();
	_Place.Init();

	CBlast::InitCBlast();
	CBullet::InitCBullet();
	CBomb::InitCBomb();
	CPlace::InitCPlace();

	CEnemy::InitCEnemy();

	SoundWave::Init();
	try {
		CTank::Init();
		CStar::InitCStar();
		CRing::InitCRing();
	}
	catch (const std::runtime_error& e) {
		MessageBoxA(NULL, e.what(), "提示", MB_OK);
		return FALSE;
	}

	_GUI = std::make_unique<GUISystem>(_Device.Get(), _WindowWidth, _WindowHeight);

	// 创建字体
	BOOL bRes = _GUI->CreateFontA("Arial", 30, arialID);
	if (!bRes)
		return FALSE;
	bRes = _GUI->CreateFontA("宋体", 40, songtiID);
	if (!bRes)
		return FALSE;
	// 显示字体
	bRes = _GUI->AddStaticText(
		STATIC_ID_1,
		"=====================================",
		0, 20,
		D3DCOLOR_XRGB(255, 0, 255), arialID);
	if (!bRes)
		return FALSE;

	bRes = _GUI->AddStaticText(
		STATIC_ID_2,
		"a左 w上 d下 s右 j发射子弹 space开始",
		20, 300,
		D3DCOLOR_XRGB(0, 255, 0), arialID);
	if (!bRes)
		return FALSE;

	bRes = _GUI->AddStaticText(
		STATIC_ID_3,
		"=====================================",
		0, 320,
		D3DCOLOR_XRGB(255, 0, 255), arialID);
	if (!bRes)
		return FALSE;

	bRes = _GUI->AddStaticText(
		STATIC_ID_4,
		"坦克大战",
		200, 80,
		D3DCOLOR_XRGB(255, 0, 0), songtiID);
	if (!bRes)
		return FALSE;

	// 图片按钮
	if (!_GUI->AddButton(BUTTON_ID_1, 210, 130,
		"./res/images/guis/startUp.png",
		"./res/images/guis/StartOver.png",
		"./res/images/guis/startDown.png")) return FALSE;

	if (!_GUI->AddButton(BUTTON_ID_2, 210, 170,
		"./res/images/guis/loadUp.png",
		"./res/images/guis/loadOver.png",
		"./res/images/guis/loadDown.png")) return FALSE;

	if (!_GUI->AddButton(BUTTON_ID_3, 210, 210,
		"./res/images/guis/optionsUp.png",
		"./res/images/guis/optionsOver.png",
		"./res/images/guis/optionsDown.png")) return FALSE;

	if (!_GUI->AddButton(BUTTON_ID_4, 210, 250,
		"./res/images/guis/quitUp.png",
		"./res/images/guis/quitOver.png",
		"./res/images/guis/quitDown.png")) return FALSE;

	return TRUE;
}

BOOL GameControl::InitD3D(HWND hWnd, HINSTANCE hInstance) {
	_hWnd = hWnd;
	_hInstance = hInstance;

	_D3D = Direct3DCreate9(D3D_SDK_VERSION);
	if (nullptr == _D3D.Get()) {
		MessageBoxA(_hWnd, "Direct3DCreate9 NULL", "提示", MB_OK);
		return FALSE;
	}
	D3DDISPLAYMODE displayMode;
	auto hr = _D3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &displayMode);
	if (FAILED(hr)) {
		MessageBoxA(_hWnd, "GetAdapterDisplayMode FAILED", "提示", MB_OK);
		return FALSE;
	}

	D3DPRESENT_PARAMETERS d3dpp{ 0 };
	d3dpp.BackBufferFormat = displayMode.Format;
	d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
	d3dpp.Windowed = TRUE;
	d3dpp.hDeviceWindow = _hWnd;// 设备窗口
	d3dpp.BackBufferWidth = _WindowWidth; // 后台缓冲区的宽度
	d3dpp.BackBufferHeight = _WindowHeight; // 后台缓冲区的高度
	d3dpp.BackBufferCount = 1;// 后台缓存数量1个
	d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE; // 呈现间隔

	hr = _D3D->CreateDevice(
		D3DADAPTER_DEFAULT,//默认显卡
		D3DDEVTYPE_HAL, //硬件图形渲染
		_hWnd,
		D3DCREATE_HARDWARE_VERTEXPROCESSING | D3DCREATE_PUREDEVICE,//硬件处理
		&d3dpp,
		&_Device
	);

	if (FAILED(hr)) {
		MessageBoxA(_hWnd, "CreateDevice FAILED", "提示", MB_OK);
		return FALSE;
	}

	if (!InitObject()) {
		return FALSE;
	}

	D3DXMATRIX matProj;
	// 正交投影
	D3DXMatrixOrthoLH(&matProj, _WindowWidth, _WindowHeight, 0.0f, 1000.0f);
	_Device->SetTransform(D3DTS_PROJECTION, &matProj);

	return TRUE;
}

VOID GameControl::GameRenderScene() {
	// D3DFVF_XYZ 需要下面设置
	_Device->SetRenderState(D3DRS_LIGHTING, FALSE);
	_Device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
	// 开启Alpha通道，开始后，带颜色的图元不显示。
	_Device->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
	//清屏
	_Device->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(50, 50, 50), 1.0f, 0);
	//开始渲染
	_Device->BeginScene();

	_Place.Draw();



	//CEnemy::ShowCEnemy();


	_Device->EndScene();
	_Device->Present(NULL, NULL, NULL, NULL);
}

VOID GameControl::MenuRenderScene() {
	GetInput();

	_Device->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
	_Device->BeginScene();

	GUISystem::ProcessGUI(_GUI.get(), LMBDown, _MouseX, _MouseY, GUICallback);

	_Device->EndScene();
	_Device->Present(NULL, NULL, NULL, NULL);
}

VOID GameControl::GameLoop() {
	_Input.UpdateDevices();

	if (GameState == 0) {
		MenuRenderScene();
	}
	else {
		GameRenderScene();
	}
}

VOID GameControl::GetInput() {
	if (_Input.KeyDown(DIK_GRAVE)) {
		SoundWave::Play(SoundWave::_EnemyBomb);
	}
	if (_Input.KeyDown(DIK_1)) {
		SoundWave::Play(SoundWave::_Start);
	}
	if (_Input.KeyDown(DIK_2)) {
		SoundWave::Play(SoundWave::_Player0Shoot0);
	}
	if (_Input.KeyDown(DIK_3)) {
		SoundWave::Play(SoundWave::_Player0Shoot1);
	}
	if (_Input.KeyDown(DIK_4)) {
		SoundWave::Play(SoundWave::_Player1Shoot0);
	}
	if (_Input.KeyDown(DIK_5)) {
		SoundWave::Play(SoundWave::_Player1Shoot1);
	}
	if (_Input.KeyDown(DIK_6)) {
		SoundWave::Play(SoundWave::_bin);
	}
	if (_Input.KeyDown(DIK_7)) {
		SoundWave::Play(SoundWave::_Background);
	}
	if (_Input.KeyDown(DIK_8)) {
		SoundWave::Play(SoundWave::_getprop);
	}
	if (_Input.KeyDown(DIK_9)) {
		SoundWave::Play(SoundWave::_propout);
	}
	if (_Input.KeyDown(DIK_0)) {
		SoundWave::Play(SoundWave::_CampBomb);
	}
	if (_Input.KeyDown(DIK_NUMPAD0)) {
		SoundWave::Play(SoundWave::_CampBomb);
	}
	if (_Input.KeyDown(DIK_NUMPAD1)) {
		SoundWave::Play(SoundWave::_scorepanel_di);
	}
	if (_Input.KeyDown(DIK_NUMPAD2)) {
		SoundWave::Play(SoundWave::_bouns1000);
	}
	if (_Input.KeyDown(DIK_NUMPAD3)) {
		SoundWave::Play(SoundWave::_addlife);
	}
	if (_Input.KeyDown(DIK_NUMPAD4)) {
		SoundWave::Play(SoundWave::_Fail);
	}
	if (_Input.KeyDown(DIK_NUMPAD5)) {
		SoundWave::Play(SoundWave::_PlayerBomb);
	}
	if (_Input.KeyDown(DIK_NUMPAD6)) {
		SoundWave::Play(SoundWave::_PlayerMove);
	}
}

VOID GameControl::GUICallback(INT id, INT state) {
	switch (id)
	{
	case BUTTON_ID_1:
		// 开始游戏
		if (state == UGP_BUTTON_DOWN) {
			GameState = 1;
			//SoundWave::Play(SoundWave::_Start);
		}
		break;
	case BUTTON_ID_2:
		break;
	case BUTTON_ID_3:
		break;
	case BUTTON_ID_4:
		if (state == UGP_BUTTON_DOWN) PostQuitMessage(0);
		break;
	}
}



